package cn.piaoyi.web.action;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import cn.piaoyi.bean.Address;
import cn.piaoyi.bean.Admin;
import cn.piaoyi.bean.Cart;
import cn.piaoyi.bean.Category;
import cn.piaoyi.bean.City;
import cn.piaoyi.bean.Goods;
import cn.piaoyi.bean.Price;
import cn.piaoyi.bean.Province;
import cn.piaoyi.bean.UserInfo;
import cn.piaoyi.comm.Constant;
import cn.piaoyi.comm.Util;
import cn.piaoyi.exception.PiaoyiException;
import cn.piaoyi.service.AddressService;
import cn.piaoyi.service.AdminService;
import cn.piaoyi.service.CartService;
import cn.piaoyi.service.CategoryService;
import cn.piaoyi.service.CityService;
import cn.piaoyi.service.GoodsService;
import cn.piaoyi.service.PosterService;
import cn.piaoyi.service.PriceService;
import cn.piaoyi.service.UserInfoService;
import cn.piaoyi.service.web.PageList;
import cn.piaoyi.service.web.Pages;
import cn.piaoyi.web.interceptor.RemoteAddrAware;
import cn.piaoyi.web.interceptor.UserSessionAware;
import cn.piaoyi.web.servlet.AdminSession;
import cn.piaoyi.web.servlet.UserSession;
import cn.piaoyi.web.ui.OptionsInt;
import cn.piaoyi.web.ui.OptionsLong;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts2.interceptor.SessionAware;

public class AddCart extends BaseAction implements SessionAware,UserSessionAware{
	private static final Log logger = LogFactory.getLog(AddCart.class);
	
	private CartService cartService;
	private PriceService priceService;

	public CartService getCartService() {
		return cartService;
	}

	public void setCartService(CartService cartService) {
		this.cartService = cartService;
	}

	public PriceService getPriceService() {
		return priceService;
	}

	public void setPriceService(PriceService priceService) {
		this.priceService = priceService;
	}
	
	private UserSession userSession;
	private Map session;

	public UserSession getUserSession() {
		return userSession;
	}

	public void setUserSession(UserSession userSession) {
		this.userSession = userSession;
	}
	
	public Map getSession() {
		return session;
	}
	
	public void setSession(Map session) {
		this.session = session;
	}

	
	private long pid;	//价格ID
	private int number;	//数量
	private Cart cart;

	public long getPid() {
		return pid;
	}

	public void setPid(long pid) {
		this.pid = pid;
	}

	public int getNumber() {
		return number;
	}

	public void setNumber(int number) {
		this.number = number;
	}

	public Cart getCart() {
		return cart;
	}

	public void setCart(Cart cart) {
		this.cart = cart;
	}

	public String execute() {
		try {
			return this.executeMethod(this.getAction());
		} catch (Exception e) {
			logger.error(e);
			return INPUT;
		}
	}

	/**
	 * 初始页
	 */
	public String index(){
		Cart c = this.getCartService().findCartByUidPid(getUserSession().getUid(), this.pid);
		if(c == null){
			c = new Cart();
			Price p = this.getPriceService().findPriceById(this.pid);
			c.setUserId(getUserSession().getUid());
			c.setGoodsId(p.getGoodsId());
			c.setDailyId(p.getDailyId());
			c.setPriceId(p.getId());
			c.setGoodsNumber(this.number);
			c.setPrice(p);
			try {
				this.getCartService().createCart(c);
			} catch (PiaoyiException e) {
				this.addActionError(this.getText("error.cart.saveerror"));
				return ERROR;
			}
		}else{
			c.setGoodsNumber(c.getGoodsNumber() + this.number);
			try {
				this.getCartService().updateCart(c);
			} catch (PiaoyiException e) {
				this.addActionError(this.getText("error.cart.updateerror"));
				return ERROR;
			}
		}
		c.setTotalPrice(c.getPrice().getPrice()*c.getGoodsNumber());
		this.setCart(c);
		return SUCCESS;
	}
	
	public String shop(){
		Cart c = this.getCartService().findCartByUidPid(getUserSession().getUid(), this.pid);
		if(c == null){
			c = new Cart();
			Price p = this.getPriceService().findPriceById(this.pid);
			c.setUserId(getUserSession().getUid());
			c.setGoodsId(p.getGoodsId());
			c.setDailyId(p.getDailyId());
			c.setPriceId(p.getId());
			c.setGoodsNumber(this.number);
			try {
				this.getCartService().createCart(c);
			} catch (PiaoyiException e) {
				this.addActionError(this.getText("error.cart.saveerror"));
				return ERROR;
			}
		}else{
			c.setGoodsNumber(c.getGoodsNumber() + this.number);
			try {
				this.getCartService().updateCart(c);
			} catch (PiaoyiException e) {
				this.addActionError(this.getText("error.cart.updateerror"));
				return ERROR;
			}
		}
		return "shopcart";
	}
}
